package com.ccut.xunfeiSpringboot.service.impl;

import cn.xfyun.api.FaceDetectClient;
import cn.xfyun.config.FaceDetectEnum;
import com.ccut.xunfeiSpringboot.common.FaceConfig;
import com.ccut.xunfeiSpringboot.service.FaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Base64;

@Service
public class FaceServiceImpl implements FaceService {

    @Autowired
    private FaceConfig faceConfig;

    @Override
    public String analyzeFace(byte[] imageData, String imageFormat) throws IOException {
        FaceDetectClient client = new FaceDetectClient.Builder(
                faceConfig.getAppId(),
                faceConfig.getFaceDetectClientApiKey(),
                faceConfig.getFaceDetectClientApiSecret()
        )
                .detectPoints(FaceDetectEnum.ON)  // 开启特征点检测
                .detectProperty(FaceDetectEnum.ON) // 开启属性分析
                .build();

        String imageBase64 = Base64.getEncoder().encodeToString(imageData);
        return client.faceContrast(imageBase64, imageFormat);
    }
}